package com.clb.toolsbox.toolsJPanel;

/**
 * author : 常利兵
 * github : <a href="https://github.com/dxmwl">github</a>
 * time   : 2025/6/7 23:03
 * file   : null.java
 * desc   :
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.function.Supplier;

public class ToolsDetailPage extends JPanel {
    private CardLayout cardLayout;
    private JPanel contentPanel;
    private JLabel titleLabel;

    public ToolsDetailPage() {
        setLayout(new BorderLayout());

        // 顶部标题栏
        JPanel headerPanel = new JPanel(new BorderLayout());
        titleLabel = new JLabel("工具详情", SwingConstants.CENTER);
        JButton backButton = new JButton("← 返回");

        backButton.addActionListener(this::onBack);

        headerPanel.add(backButton, BorderLayout.WEST);
        headerPanel.add(titleLabel, BorderLayout.CENTER);
        headerPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        add(headerPanel, BorderLayout.NORTH);

        // 内容区域
        cardLayout = new CardLayout();
        contentPanel = new JPanel(cardLayout);
        add(contentPanel, BorderLayout.CENTER);
    }

    public void showTool(String toolName, Supplier<JComponent> toolSupplier) {
        titleLabel.setText(toolName);
        JComponent toolComponent = toolSupplier.get();
        String panelName = toolName;
        contentPanel.add(toolComponent, panelName);
        cardLayout.show(contentPanel, panelName);
    }

    private void onBack(ActionEvent e) {
        if (toolCallback != null) {
            toolCallback.onBackToToolList();
        }
    }

    private ToolCallback toolCallback;

    public interface ToolCallback {
        void onBackToToolList();
    }

    public void setToolCallback(ToolCallback callback) {
        this.toolCallback = callback;
    }
}
